package com.blog.service.Impl;

import com.blog.context.BaseContext;
import com.blog.dto.ExperienceDTO;
import com.blog.entity.Experience;
import com.blog.mapper.ExperienceMapper;
import com.blog.service.ExperienceService;
import com.blog.utils.StrUtil;
import com.blog.vo.ExperienceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExperienceServiceImpl implements ExperienceService {

    @Autowired
    private ExperienceMapper experienceMapper;
    //后台系统Token获取
    @Override
    public List<ExperienceVO> listByUId() {
        List<ExperienceVO> experienceVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<Experience> list = experienceMapper.select(currentId);
        if (list.size() != 0) {
            list.forEach(experience -> {
                ExperienceVO experienceVO = new ExperienceVO();
                BeanUtils.copyProperties(experience, experienceVO);
                experienceVO.setStartDate(StrUtil.formattedTime(experience.getStartDate()));
                experienceVO.setEndDate(StrUtil.formattedTime(experience.getEndDate()));
                experienceVOList.add(experienceVO);
            });
        }
        return experienceVOList;
    }
    //前台系统传入UID获取
    @Override
    public List<ExperienceVO> listBySendUId(Long uid) {
        List<ExperienceVO> experienceVOList = new ArrayList<>();
        List<Experience> list = experienceMapper.select(uid);
        if (list.size() != 0) {
            list.forEach(experience -> {
                ExperienceVO experienceVO = new ExperienceVO();
                BeanUtils.copyProperties(experience, experienceVO);
                experienceVO.setStartDate(StrUtil.formattedTime(experience.getStartDate()));
                experienceVO.setEndDate(StrUtil.formattedTime(experience.getEndDate()));
                experienceVOList.add(experienceVO);
            });
        }
        return experienceVOList;
    }


    @Override
    public List<ExperienceVO> listByTitel(String value) {
        List<ExperienceVO> experienceVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<Experience> list = experienceMapper.selectByValue(currentId, value);
        if (list.size() != 0) {
            list.forEach(experience -> {
                ExperienceVO experienceVO = new ExperienceVO();
                BeanUtils.copyProperties(experience, experienceVO);
                experienceVO.setStartDate(StrUtil.formattedTime(experience.getStartDate()));
                experienceVO.setEndDate(StrUtil.formattedTime(experience.getEndDate()));
                experienceVOList.add(experienceVO);
            });
        }
        return experienceVOList;
    }

    @Override
    public ExperienceVO getOneExperienceById(Long eId) {
        Experience experience = experienceMapper.selectByID(eId);
        ExperienceVO experienceVO = new ExperienceVO();
        BeanUtils.copyProperties(experience, experienceVO);
        experienceVO.setStartDate(StrUtil.formattedTime(experience.getStartDate()));
        experienceVO.setEndDate(StrUtil.formattedTime(experience.getEndDate()));
        return experienceVO;
    }

    @Override
    public boolean AddExperience(ExperienceDTO experienceDTO) {
        Long currentId = BaseContext.getCurrentId();
        Experience experience = new Experience();
        BeanUtils.copyProperties(experienceDTO, experience);
        experience.setStartDate(StrUtil.getFormattedTimestamp(experienceDTO.getStartDate()));
        experience.setEndDate(StrUtil.getFormattedTimestamp(experienceDTO.getEndDate()));
        experience.setUserId(currentId);
        experienceMapper.insert(experience);
        return true;
    }

    @Override
    public void EditExperience(ExperienceDTO experienceDTO) {
        Experience experience = new Experience();
        BeanUtils.copyProperties(experienceDTO, experience);
        experience.setStartDate(StrUtil.getFormattedTimestamp(experienceDTO.getStartDate()));
        experience.setEndDate(StrUtil.getFormattedTimestamp(experienceDTO.getEndDate()));
        experienceMapper.update(experience);
    }

    @Override
    public void DelExperience(Long id) {
        experienceMapper.delete(id);
    }



}
